package com.xt.purchasingsystem.controller;

import com.xt.purchasingsystem.utils.FileUtil;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value = "/file")
@Api(description = "文件管理API")
public class FileController {

    @ApiOperation(value = "根据文件地址删除文件", notes = "根据文件地址删除文件")
    @DeleteMapping("/delete")
    public Result delete( @ApiParam(name = "filePath", value = "文件地址", required = true) String filePath) {
        if (filePath.length() > 0) {
            if (filePath.indexOf("images/user/")!=-1){
                boolean flag =
                        FileUtil.deleteFile(filePath);
                if (flag) {
                    return ResultFactory.buildSuccessResult("删除图片成功");
                } else {
                    return ResultFactory.buildFailResult("删除图片失败");
                }
            }

        }
        return null;
    }


}
